package com.wushijia.classload;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Paths;

public class MyClassLoad extends ClassLoader {


  public void findDeprecatedMethod(Class<?> clazz) {
    //找出已过时方法
    for (Method method : clazz.getMethods()) {
      for (Annotation annotation : method.getAnnotations()) {
        if (annotation.annotationType() == Deprecated.class) {
          System.out.println(method.getName());
        }
      }
    }
  }

  public Class<?> loadFromDisk(String pathName) throws IOException {
    byte[] bytes = Files.readAllBytes(Paths.get(pathName));
    return defineClass(null, bytes, 0, bytes.length);
  }


  public static void main(String[] args) throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException {
    //找出该文件夹下ClassTest类中的过时方法
    MyClassLoad myClassLoad = new MyClassLoad();
    String path = MyClassLoad.class.getResource("").getFile().substring(1);
    Class<?> clazz = myClassLoad.loadFromDisk(path + "ClassTest.class");
    myClassLoad.findDeprecatedMethod(clazz);

    //类加载器
    String clazz2 = "com.wushijia.classload.ClassTest";
    Object o = Class.forName(clazz2).newInstance();
    System.out.println(o.toString());
  }

}
